// models/field_info.dart
import 'package:json_annotation/json_annotation.dart';
import 'package:uuid/uuid.dart';
part 'field_info.g.dart'; // 导入生成的代码

enum FieldType { string, number, boolean, object, array, nullType, unknown }

@JsonSerializable()
class FieldInfo {
  final String id;
  late final String name;
  final FieldType type;
  final String? description;
  final List<FieldInfo>? children;
  final dynamic value;
  final bool isRequired;

  FieldInfo({
    String? id,
    required this.name,
    required this.type,
    this.description,
    this.children,
    this.value,
    this.isRequired = false,
  }) : id = id ?? Uuid().v4();

  factory FieldInfo.fromJson(Map<String, dynamic> json) =>
      _$FieldInfoFromJson(json);
  Map<String, dynamic> toJson() => _$FieldInfoToJson(this);

  FieldInfo copyWith({
    String? id,
    String? name,
    FieldType? type,
    String? description,
    List<FieldInfo>? children,
    dynamic value,
    bool? isRequired,
  }) {
    return FieldInfo(
      id: id ?? this.id,
      name: name ?? this.name,
      type: type ?? this.type,
      description: description ?? this.description,
      children: children ?? this.children,
      value: value ?? this.value,
      isRequired: isRequired ?? this.isRequired,
    );
  }

  String get typeString {
    switch (type) {
      case FieldType.string:
        return 'String';
      case FieldType.number:
        return 'Number';
      case FieldType.boolean:
        return 'Boolean';
      case FieldType.object:
        return 'Object';
      case FieldType.array:
        return 'Array';
      case FieldType.nullType:
        return 'Null';
      default:
        return 'Unknown';
    }
  }

  Map<String, dynamic> toServerJson() {
    // 类型映射表，将我们的类型字符串映射为服务器期望的
    final typeMap = {
      'String': 'string',
      'Number': 'number',
      'Boolean': 'bool',
      'Object': 'object',
      'Array': 'array',
      'Null': 'null',
      'Unknown': 'unknown',
    };

    return {
      'name': name,
      'type': typeMap[typeString] ?? 'unknown',
      'desc': description,
      'value': value,
      'children': children?.map((child) => child.toServerJson()).toList(),
    };
  }

  // 在FieldInfo模型中的toApiFormat方法
  Map<String, dynamic> toApiFormat() {
    return {
      'name': name,
      'type': typeString,
      'desc': description ?? '', // 确保备注被包含
      'is_required': isRequired,
      'value': value,
      'children': children?.map((child) => child.toApiFormat()).toList(),
    };
  }

  // fromApiFormat方法需要正确解析所有字段
  static FieldInfo fromApiFormat(Map<String, dynamic> data) {
    return FieldInfo(
      name: data['name'] ?? '',
      type: _parseTypeFromString(data['type'] ?? 'string'),
      description: data['desc'] ?? '', // 确保解析备注字段
      isRequired: data['is_required'] ?? false,
      value: data['value'],
      children: data['children'] != null
          ? (data['children'] as List)
                .map((child) => FieldInfo.fromApiFormat(child))
                .toList()
          : null,
    );
  }

  static FieldType _parseTypeFromString(String typeStr) {
    switch (typeStr) {
      case 'String':
        return FieldType.string;
      case 'Number':
        return FieldType.number;
      case 'Boolean':
        return FieldType.boolean;
      case 'Object':
        return FieldType.object;
      case 'Array':
        return FieldType.array;
      case 'Null':
        return FieldType.nullType;
      default:
        return FieldType.unknown;
    }
  }
}
